package com.example.demo.config;


import com.example.demo.enums.ResultCodeEnum;
import com.example.demo.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

//统一异常处理
@RestControllerAdvice
public class ExceptionAdvice {

    @Autowired
    private static Logger log = LoggerFactory.getLogger(ExceptionAdvice.class);

    @ExceptionHandler
    public Result handler(NoResourceFoundException e)
    {
        log.error("发生异常,e:",e.getResourcePath());
        return Result.fail(ResultCodeEnum.NO_RESOURCES.getCode(), e.getResourcePath());
    }

    @ExceptionHandler //参数不合法异常
    public Result handler(UnsupportedOperationException e)
    {
        log.error("发送异常,e:",e.getMessage());
        return Result.fail(ResultCodeEnum.PARAM_INVALID.getCode(),e.getMessage());
    }

    @ExceptionHandler
    public Result handler(Exception e)
    {
        log.error("发生异常,e:",e.getMessage());
        return Result.fail(e.getMessage());
    }



}
